﻿using System;
using System.Drawing;
using System.IO;
using System.Net;
using System.Threading;
using System.Xml;
using System.Windows.Forms;
using System.ComponentModel;

namespace PublicTransport.Views
{
    public partial class MapView : Form
    {
        private int actualMapType = 0;
        private GpsMapImage mapImage = null;
        private GpsPoint finishPoint;
        private GpsPoint actualPoint;
        private int width;
        private int height;
        private bool formVisible = false;
        private bool guiEnabled = false;
        private bool tryingToConnect = false;
        
        public MapView(GpsPoint finish)
        {
            InitializeComponent();
            setEnabledAllGui(false);
            width = pictureBoxMap.Size.Width;
            height = pictureBoxMap.Size.Height;
            finishPoint = finish;
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.Default;
            this.Close();
        }

        public void setActualCoordinates(GpsPoint point)
        {
            actualPoint = point;
            if (mapImage == null && !tryingToConnect)
            {
                ThreadStart thread = () =>
                    {
                        tryingToConnect = true;
                        switch (actualMapType)
                        {
                            case 0: mapImage = new GpsMapImageGoogle(point, finishPoint, width, height); break;
                            case 1: mapImage = new GpsMapImageBing(point, finishPoint, width, height); break;
                            case 2: mapImage = new GpsMapImageOsm(point, finishPoint, width, height); break;
                        }
                    };
                thread.Invoke();
            }
        }

        private void timer_refresh_Tick(object sender, EventArgs e)
        {
            if (mapImage != null && mapImage.finished)
            {
                setEnabledAllGui(true);
                if (mapImage.ok)
                {
                    lbl_status.Visible = false;
                    radioButtonMarker.Visible = true;
                    if (mapImage.newImage) pictureBoxMap.Image = mapImage.image;
                    if (actualPoint != null)
                    {
                        Point radioButtonPoint = mapImage.degreesToPixelsFromUpperLeft(actualPoint);
                        radioButtonPoint.X -= (int)Math.Round((double)radioButtonMarker.Width / 2);
                        radioButtonPoint.Y -= (int)Math.Round((double)radioButtonMarker.Height / 2);
                        if (((radioButtonPoint.X + (int)Math.Round((double)radioButtonMarker.Width) > pictureBoxMap.Image.Width)) 
                            || ((radioButtonPoint.X < 0))
                            || ((radioButtonPoint.Y + (int)Math.Round((double)radioButtonMarker.Height) > pictureBoxMap.Image.Height))
                            || ((radioButtonPoint.Y < 0)))
                            radioButtonMarker.Visible = false;
                        else
                        {
                            radioButtonMarker.Location = radioButtonPoint;
                            radioButtonMarker.Visible = true;
                        }
                    }
                }
                else
                {
                    lbl_status.Text = "Nebolo možné načítať mapu.";
                    lbl_status.Visible = true;
                    mapImage = null;
                    tryingToConnect = false;
                }
            }
            else if (mapImage == null)
            {
                setEnabledAllGui(false);
            }
        }

        private void setEnabledAllGui(bool enabled)
        {
            guiEnabled = enabled;
            if (formVisible) Cursor.Current = (guiEnabled ? Cursors.Default : Cursors.WaitCursor);

            btn_down.Enabled = enabled;
            btn_left.Enabled = enabled;
            btn_minus.Enabled = enabled;
            btn_plus.Enabled = enabled;
            btn_up.Enabled = enabled;
            btn_right.Enabled = enabled;
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            setEnabledAllGui(false);
            mapImage = null;
            tryingToConnect = false;
            lbl_status.Text = "Mapa sa načítava...";
            lbl_status.Visible = true;
            radioButtonMarker.Visible = false;
        }

        private void btn_minus_Click(object sender, EventArgs e)
        {
            lbl_status.Text = "Mapa sa načítava...";
            lbl_status.Visible = true;
            setEnabledAllGui(false);
            mapImage.changeZoom(false);
        }

        private void btn_plus_Click(object sender, EventArgs e)
        {
            lbl_status.Text = "Mapa sa načítava...";
            lbl_status.Visible = true;
            setEnabledAllGui(false);
            mapImage.changeZoom(true);
        }

        private void btn_up_Click(object sender, EventArgs e)
        {
            lbl_status.Text = "Mapa sa načítava...";
            lbl_status.Visible = true;
            setEnabledAllGui(false);
            mapImage.changeCenter(1);
        }

        private void btn_left_Click(object sender, EventArgs e)
        {
            lbl_status.Text = "Mapa sa načítava...";
            lbl_status.Visible = true;
            setEnabledAllGui(false);
            mapImage.changeCenter(2);
        }

        private void btn_right_Click(object sender, EventArgs e)
        {
            lbl_status.Text = "Mapa sa načítava...";
            lbl_status.Visible = true;
            setEnabledAllGui(false);
            mapImage.changeCenter(0);
        }

        private void btn_down_Click(object sender, EventArgs e)
        {
            lbl_status.Text = "Mapa sa načítava...";
            lbl_status.Visible = true;
            setEnabledAllGui(false);
            mapImage.changeCenter(3);
        }

        private void MapView_Closing(object sender, CancelEventArgs e)
        {
            formVisible = false;
            Cursor.Current = Cursors.Default;
        }

        private void MapView_Activated(object sender, EventArgs e)
        {
            formVisible = true;
            Cursor.Current = (guiEnabled ? Cursors.Default : Cursors.WaitCursor);
        }

        public void setMapSource(int mapType)
        {
            actualMapType = mapType;
            mapImage = null;
            tryingToConnect = false;
        }

        private void btn_mapType_Click(object sender, EventArgs e)
        {
            if (mapImage != null)
            {
                mapImage.changeMapType();
            }
        }
    }
}